# Load required packages

library(ggplot2)

# Set working directory

setwd("~/Dropbox/Shared_Al_Adam/APR Replication Data")

# Read in data

data <- read.csv("fang_thal_apr_replication_final.csv")

#########################
# Create sanction index #
#########################

data$sanc_index <- apply(data[,c("sanc_viol", "sanc_viol_press", "sanc_ownparty", "sanc_bizcoi")], 1, function(J) mean(J, na.rm=TRUE))

############
# Figure 1 #
############

# Create data frame of estimates for plot

rep_approval <- lm(disapprove ~ treat_source, data = data[data$s_pid3 == "Republican",])
rep_approval_elites_coef <- coef(rep_approval)[2]
rep_approval_elites_pval <- summary(rep_approval)$coefficients[2,4]
rep_approval_elites_low95 <- confint(rep_approval)[2,1]
rep_approval_elites_hi95 <- confint(rep_approval)[2,2]
rep_approval_voters_coef <- coef(rep_approval)[3]
rep_approval_voters_pval <- summary(rep_approval)$coefficients[3,4]
rep_approval_voters_low95 <- confint(rep_approval)[3,1]
rep_approval_voters_hi95 <- confint(rep_approval)[3,2]

rep_chal <- lm(t_chal ~ treat_source, data = data[data$s_pid3 == "Republican",])
rep_chal_elites_coef <- coef(rep_chal)[2]
rep_chal_elites_pval <- summary(rep_chal)$coefficients[2,4]
rep_chal_elites_low95 <- confint(rep_chal)[2,1]
rep_chal_elites_hi95 <- confint(rep_chal)[2,2]
rep_chal_voters_coef <- coef(rep_chal)[3]
rep_chal_voters_pval <- summary(rep_chal)$coefficients[3,4]
rep_chal_voters_low95 <- confint(rep_chal)[3,1]
rep_chal_voters_hi95 <- confint(rep_chal)[3,2]

rep_nom <- lm(t_nom ~ treat_source, data = data[data$s_pid3 == "Republican",])
rep_nom_elites_coef <- coef(rep_nom)[2]
rep_nom_elites_pval <- summary(rep_nom)$coefficients[2,4]
rep_nom_elites_low95 <- confint(rep_nom)[2,1]
rep_nom_elites_hi95 <- confint(rep_nom)[2,2]
rep_nom_voters_coef <- coef(rep_nom)[3]
rep_nom_voters_pval <- summary(rep_nom)$coefficients[3,4]
rep_nom_voters_low95 <- confint(rep_nom)[3,1]
rep_nom_voters_hi95 <- confint(rep_nom)[3,2]

rep_index <- lm(sanc_index ~ treat_source, data = data[data$s_pid3 == "Republican",])
rep_index_elites_coef <- coef(rep_index)[2]
rep_index_elites_pval <- summary(rep_index)$coefficients[2,4]
rep_index_elites_low95 <- confint(rep_index)[2,1]
rep_index_elites_hi95 <- confint(rep_index)[2,2]
rep_index_voters_coef <- coef(rep_index)[3]
rep_index_voters_pval <- summary(rep_index)$coefficients[3,4]
rep_index_voters_low95 <- confint(rep_index)[3,1]
rep_index_voters_hi95 <- confint(rep_index)[3,2]

pid <- rep("Republicans",8)
rep_df <- as.data.frame(pid)
rep_df$var <- c("Trump Disapproval",
								"Trump Disapproval",
                "Wants Trump to Face\nViable Primary\nChallengers in 2020",
								"Wants Trump to Face\nViable Primary\nChallengers in 2020",
                "Doesn't Want Trump as\n2020 Republican\nPresidential Nominee",
								"Doesn't Want Trump as\n2020 Republican\nPresidential Nominee",
                "Pro-Sanction\nIndex Measure",
								 "Pro-Sanction\nIndex Measure")
rep_df$type <- c(rep(c("Elites","Voters"),4))
rep_df$coef <- c(rep_approval_elites_coef,
								 rep_approval_voters_coef,
								 rep_chal_elites_coef,
								 rep_chal_voters_coef,
								 rep_nom_elites_coef,
								 rep_nom_voters_coef,
								 rep_index_elites_coef,
								 rep_index_voters_coef)
rep_df$pval <- c(rep_approval_elites_pval,
								 rep_approval_voters_pval,
								 rep_chal_elites_pval,
								 rep_chal_voters_pval,
								 rep_nom_elites_pval,
								 rep_nom_voters_pval,
								 rep_index_elites_pval,
								 rep_index_voters_pval)
rep_df$low95 <- c(rep_approval_elites_low95,
								 rep_approval_voters_low95,
								 rep_chal_elites_low95,
								 rep_chal_voters_low95,
								 rep_nom_elites_low95,
								 rep_nom_voters_low95,
								 rep_index_elites_low95,
								 rep_index_voters_low95)
rep_df$hi95 <- c(rep_approval_elites_hi95,
								 rep_approval_voters_hi95,
								 rep_chal_elites_hi95,
								 rep_chal_voters_hi95,
								 rep_nom_elites_hi95,
								 rep_nom_voters_hi95,
								 rep_index_elites_hi95,
								 rep_index_voters_hi95)

# Create plot

rep_df$sig_color <- "ns"
rep_df$sig_color <- ifelse(rep_df$pval <= .1, "10percent", rep_df$sig_color)
rep_df$sig_color <- ifelse(rep_df$pval <= .05, "5percent", rep_df$sig_color)

rep_df$var <- factor(rep_df$var, 
											levels = c("Trump Disapproval",
																 "Wants Trump to Face\nViable Primary\nChallengers in 2020",
																 "Doesn't Want Trump as\n2020 Republican\nPresidential Nominee",
																 "Pro-Sanction\nIndex Measure"))

ggplot(rep_df, aes(x=type, y=coef, color=sig_color, shape=sig_color)) +
	geom_hline(yintercept = 0, lty = 2, color = "gray") +
	geom_point(size=3) + 
	geom_errorbar(aes(ymin = low95, ymax = hi95), width = .1) +
	xlab('Appeal Source (Co-Partisan)') +
	ylab('Estimated Treatment Effect') +
	facet_grid(. ~ var) +
	scale_shape_manual(values = c(2,1,16), 
										 labels = c("10percent", "5percent", "ns")) +
	scale_color_manual(values = c("black", "black", "gray55"), 
										 labels = c("10percent","5percent", "ns")) +
	scale_y_continuous(limits = c(-.16,.16),
										 breaks = seq(-.15,.15,.05),
										 labels = c("-.15","-.10","-.05", "0",
										 					 ".05",".10",".15")) +
	theme_bw() +
	theme(legend.position = "none") +
	labs(caption = "Note: Republican only.")

############
# Figure 2 #
############

# Create data frame of estimates for plot

rep_approval <- lm(disapprove ~ treat_source_appeal, data = data[data$s_pid3 == "Republican",])
rep_approval_elites_unethical_coef <- coef(rep_approval)[2]
rep_approval_elites_unethical_pval <- summary(rep_approval)$coefficients[2,4]
rep_approval_elites_unethical_low95 <- confint(rep_approval)[2,1]
rep_approval_elites_unethical_hi95 <- confint(rep_approval)[2,2]
rep_approval_elites_unamerican_coef <- coef(rep_approval)[3]
rep_approval_elites_unamerican_pval <- summary(rep_approval)$coefficients[3,4]
rep_approval_elites_unamerican_low95 <- confint(rep_approval)[3,1]
rep_approval_elites_unamerican_hi95 <- confint(rep_approval)[3,2]
rep_approval_voters_unethical_coef <- coef(rep_approval)[4]
rep_approval_voters_unethical_pval <- summary(rep_approval)$coefficients[4,4]
rep_approval_voters_unethical_low95 <- confint(rep_approval)[4,1]
rep_approval_voters_unethical_hi95 <- confint(rep_approval)[4,2]
rep_approval_voters_unamerican_coef <- coef(rep_approval)[5]
rep_approval_voters_unamerican_pval <- summary(rep_approval)$coefficients[5,4]
rep_approval_voters_unamerican_low95 <- confint(rep_approval)[5,1]
rep_approval_voters_unamerican_hi95 <- confint(rep_approval)[5,2]

rep_chal <- lm(t_chal ~ treat_source_appeal, data = data[data$s_pid3 == "Republican",])
rep_chal_elites_unethical_coef <- coef(rep_chal)[2]
rep_chal_elites_unethical_pval <- summary(rep_chal)$coefficients[2,4]
rep_chal_elites_unethical_low95 <- confint(rep_chal)[2,1]
rep_chal_elites_unethical_hi95 <- confint(rep_chal)[2,2]
rep_chal_elites_unamerican_coef <- coef(rep_chal)[3]
rep_chal_elites_unamerican_pval <- summary(rep_chal)$coefficients[3,4]
rep_chal_elites_unamerican_low95 <- confint(rep_chal)[3,1]
rep_chal_elites_unamerican_hi95 <- confint(rep_chal)[3,2]
rep_chal_voters_unethical_coef <- coef(rep_chal)[4]
rep_chal_voters_unethical_pval <- summary(rep_chal)$coefficients[4,4]
rep_chal_voters_unethical_low95 <- confint(rep_chal)[4,1]
rep_chal_voters_unethical_hi95 <- confint(rep_chal)[4,2]
rep_chal_voters_unamerican_coef <- coef(rep_chal)[5]
rep_chal_voters_unamerican_pval <- summary(rep_chal)$coefficients[5,4]
rep_chal_voters_unamerican_low95 <- confint(rep_chal)[5,1]
rep_chal_voters_unamerican_hi95 <- confint(rep_chal)[5,2]

rep_nom <- lm(t_nom ~ treat_source_appeal, data = data[data$s_pid3 == "Republican",])
rep_nom_elites_unethical_coef <- coef(rep_nom)[2]
rep_nom_elites_unethical_pval <- summary(rep_nom)$coefficients[2,4]
rep_nom_elites_unethical_low95 <- confint(rep_nom)[2,1]
rep_nom_elites_unethical_hi95 <- confint(rep_nom)[2,2]
rep_nom_elites_unamerican_coef <- coef(rep_nom)[3]
rep_nom_elites_unamerican_pval <- summary(rep_nom)$coefficients[3,4]
rep_nom_elites_unamerican_low95 <- confint(rep_nom)[3,1]
rep_nom_elites_unamerican_hi95 <- confint(rep_nom)[3,2]
rep_nom_voters_unethical_coef <- coef(rep_nom)[4]
rep_nom_voters_unethical_pval <- summary(rep_nom)$coefficients[4,4]
rep_nom_voters_unethical_low95 <- confint(rep_nom)[4,1]
rep_nom_voters_unethical_hi95 <- confint(rep_nom)[4,2]
rep_nom_voters_unamerican_coef <- coef(rep_nom)[5]
rep_nom_voters_unamerican_pval <- summary(rep_nom)$coefficients[5,4]
rep_nom_voters_unamerican_low95 <- confint(rep_nom)[5,1]
rep_nom_voters_unamerican_hi95 <- confint(rep_nom)[5,2]

rep_index <- lm(sanc_index ~ treat_source_appeal, data = data[data$s_pid3 == "Republican",])
rep_index_elites_unethical_coef <- coef(rep_index)[2]
rep_index_elites_unethical_pval <- summary(rep_index)$coefficients[2,4]
rep_index_elites_unethical_low95 <- confint(rep_index)[2,1]
rep_index_elites_unethical_hi95 <- confint(rep_index)[2,2]
rep_index_elites_unamerican_coef <- coef(rep_index)[3]
rep_index_elites_unamerican_pval <- summary(rep_index)$coefficients[3,4]
rep_index_elites_unamerican_low95 <- confint(rep_index)[3,1]
rep_index_elites_unamerican_hi95 <- confint(rep_index)[3,2]
rep_index_voters_unethical_coef <- coef(rep_index)[4]
rep_index_voters_unethical_pval <- summary(rep_index)$coefficients[4,4]
rep_index_voters_unethical_low95 <- confint(rep_index)[4,1]
rep_index_voters_unethical_hi95 <- confint(rep_index)[4,2]
rep_index_voters_unamerican_coef <- coef(rep_index)[5]
rep_index_voters_unamerican_pval <- summary(rep_index)$coefficients[5,4]
rep_index_voters_unamerican_low95 <- confint(rep_index)[5,1]
rep_index_voters_unamerican_hi95 <- confint(rep_index)[5,2]

pid <- rep("Republicans",16)
rep_df <- as.data.frame(pid)
rep_df$var <- c("Trump Disapproval",
								"Trump Disapproval",
								"Trump Disapproval",
								"Trump Disapproval",
								"Wants Trump to Face\nViable Primary\nChallengers in 2020",
								"Wants Trump to Face\nViable Primary\nChallengers in 2020",
								"Wants Trump to Face\nViable Primary\nChallengers in 2020",
								"Wants Trump to Face\nViable Primary\nChallengers in 2020",
								"Doesn't Want Trump as\n2020 Republican\nPresidential Nominee",
								"Doesn't Want Trump as\n2020 Republican\nPresidential Nominee",
								"Doesn't Want Trump as\n2020 Republican\nPresidential Nominee",
								"Doesn't Want Trump as\n2020 Republican\nPresidential Nominee",
								"Pro-Sanction\nIndex Measure",
								"Pro-Sanction\nIndex Measure",
								"Pro-Sanction\nIndex Measure",
								"Pro-Sanction\nIndex Measure")
rep_df$type <- c(rep(c("Elite + Unethical",
											 "Elite + Un-American",
											 "Voter + Unethical",
											 "Voter + Un-American"),4))
rep_df$coef <- c(rep_approval_elites_unethical_coef,
								 rep_approval_elites_unamerican_coef,
								 rep_approval_voters_unethical_coef,
								 rep_approval_voters_unamerican_coef,
								 rep_chal_elites_unethical_coef,
								 rep_chal_elites_unamerican_coef,
								 rep_chal_voters_unethical_coef,
								 rep_chal_voters_unamerican_coef,
								 rep_nom_elites_unethical_coef,
								 rep_nom_elites_unamerican_coef,
								 rep_nom_voters_unethical_coef,
								 rep_nom_voters_unamerican_coef,
								 rep_index_elites_unethical_coef,
								 rep_index_elites_unamerican_coef,
								 rep_index_voters_unethical_coef,
								 rep_index_voters_unamerican_coef)
rep_df$pval <- c(rep_approval_elites_unethical_pval,
								 rep_approval_elites_unamerican_pval,
								 rep_approval_voters_unethical_pval,
								 rep_approval_voters_unamerican_pval,
								 rep_chal_elites_unethical_pval,
								 rep_chal_elites_unamerican_pval,
								 rep_chal_voters_unethical_pval,
								 rep_chal_voters_unamerican_pval,
								 rep_nom_elites_unethical_pval,
								 rep_nom_elites_unamerican_pval,
								 rep_nom_voters_unethical_pval,
								 rep_nom_voters_unamerican_pval,
								 rep_index_elites_unethical_pval,
								 rep_index_elites_unamerican_pval,
								 rep_index_voters_unethical_pval,
								 rep_index_voters_unamerican_pval)
rep_df$low95 <- c(rep_approval_elites_unethical_low95,
									rep_approval_elites_unamerican_low95,
									rep_approval_voters_unethical_low95,
									rep_approval_voters_unamerican_low95,
									rep_chal_elites_unethical_low95,
									rep_chal_elites_unamerican_low95,
									rep_chal_voters_unethical_low95,
									rep_chal_voters_unamerican_low95,
									rep_nom_elites_unethical_low95,
									rep_nom_elites_unamerican_low95,
									rep_nom_voters_unethical_low95,
									rep_nom_voters_unamerican_low95,
									rep_index_elites_unethical_low95,
									rep_index_elites_unamerican_low95,
									rep_index_voters_unethical_low95,
									rep_index_voters_unamerican_low95)
rep_df$hi95 <- c(rep_approval_elites_unethical_hi95,
								 rep_approval_elites_unamerican_hi95,
								 rep_approval_voters_unethical_hi95,
								 rep_approval_voters_unamerican_hi95,
								 rep_chal_elites_unethical_hi95,
								 rep_chal_elites_unamerican_hi95,
								 rep_chal_voters_unethical_hi95,
								 rep_chal_voters_unamerican_hi95,
								 rep_nom_elites_unethical_hi95,
								 rep_nom_elites_unamerican_hi95,
								 rep_nom_voters_unethical_hi95,
								 rep_nom_voters_unamerican_hi95,
								 rep_index_elites_unethical_hi95,
								 rep_index_elites_unamerican_hi95,
								 rep_index_voters_unethical_hi95,
								 rep_index_voters_unamerican_hi95)

# Create plot

rep_df$sig_color <- "ns"
rep_df$sig_color <- ifelse(rep_df$pval <= .1, "10percent", rep_df$sig_color)
rep_df$sig_color <- ifelse(rep_df$pval <= .05, "5percent", rep_df$sig_color)

rep_df$var <- factor(rep_df$var, 
											levels = c("Trump Disapproval",
																 "Wants Trump to Face\nViable Primary\nChallengers in 2020",
																 "Doesn't Want Trump as\n2020 Republican\nPresidential Nominee",
																 "Pro-Sanction\nIndex Measure"))

ggplot(rep_df, aes(x=type, y=coef, color=sig_color, shape=sig_color)) +
	geom_hline(yintercept = 0, lty = 2, color = "gray") +
	geom_point(size=3) + 
	geom_errorbar(aes(ymin = low95, ymax = hi95), width = .1) +
	xlab('Appeal Source and Type') +
	ylab('Estimated Treatment Effect') +
	facet_grid(. ~ var) +
	scale_shape_manual(values = c(2,1,16), 
										 labels = c("10percent","5percent", "ns")) +
	scale_color_manual(values = c("black","black", "gray55"), 
										 labels = c("10percent","5percent", "ns")) +
	scale_y_continuous(limits = c(-.16,.16),
										 breaks = seq(-.15,.15,.05),
										 labels = c("-.15","-.10","-.05", "0",
										 					 ".05",".10",".15")) +
	theme_bw() +
	theme(legend.position = "none",
				text = element_text(size=15),
				axis.text.x = element_text(angle = 45, hjust = 1, size = 10)) +
	labs(caption = "Note: Republicans only.")
